package com.zzz.system.controller.exam;

import com.zzz.common.core.controller.BaseController;
import com.zzz.common.core.domain.R;
import com.zzz.common.core.domain.TableDataInfo;
import com.zzz.system.domain.exam.dto.ExamAddDTO;
import com.zzz.system.domain.exam.dto.ExamEditDTO;
import com.zzz.system.domain.exam.dto.ExamQueryDTO;
import com.zzz.system.domain.exam.dto.ExamQuestionAddDTO;
import com.zzz.system.domain.exam.vo.ExamDetailVO;
import com.zzz.system.service.exam.ExamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.system.controller.exam
 * @Author: zhai
 * @CreateTime: 2025-10-19  16:26
 * @Description: TODO 竞赛管理
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "竞赛管理")
@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private ExamService examService;

    @GetMapping("/list")
    @Operation(summary = "查询竞赛列表", description = "查询竞赛列表")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        log.info("查询竞赛列表");
        return getDataTable(examService.list(examQueryDTO));
    }

    @PostMapping("/add")
    @Operation(summary = "添加竞赛", description = "添加竞赛")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> add(@Validated @RequestBody ExamAddDTO examAddDTO) {
        log.info("添加竞赛");
        return toR(examService.add(examAddDTO));
    }

    @PostMapping("/question/add")
    @Operation(summary = "添加竞赛题目", description = "添加竞赛题目")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> addExamQuestion(@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        log.info("添加竞赛题目");
        return toR(examService.addExamQuestion(examQuestionAddDTO));
    }


    @PutMapping("/edit")
    @Operation(summary = "编辑竞赛", description = "编辑竞赛")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> edit(@RequestBody ExamEditDTO examEditDTO) {
        return toR(examService.edit(examEditDTO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除竞赛", description = "删除竞赛")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> delete(Long examId) {
        log.info("删除竞赛: {}", examId);
        return toR(examService.delete(examId));
    }

    @DeleteMapping("/question/delete")
    @Operation(summary = "删除竞赛赛题", description = "删除竞赛赛题")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> questionDelete(Long examId, Long questionId) {
        return toR(examService.questionDelete(examId, questionId));
    }

    @GetMapping("/detail")
    @Operation(summary = "查询竞赛详情", description = "查询竞赛详情")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<ExamDetailVO> detail(Long examId) {
        log.info("查询竞赛详情: {}", examId);
        return R.success(examService.detail(examId));
    }

    @PutMapping("/publish")
    public R<Void> publish(Long examId) {
        return toR(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    public R<Void> cancelPublish(Long examId) {
        return toR(examService.cancelPublish(examId));
    }
}
